package org.victoria.market.service.imp;

import com.alibaba.fastjson.JSON;
import org.apache.thrift.TException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.victoria.market.api.ICategory;
import org.victoria.market.dao.DaoSupport;
import org.victoria.market.domain.Category;
import org.victoria.market.utils.QueryHelper;

import java.util.List;

/**
 * Created by thanatos on 15-10-2.
 */
@Service
@Transactional(rollbackFor = TException.class)
public class ICategoryImp implements ICategory.Iface{

    private static final Logger logger = LoggerFactory.getLogger("ICategoryImp");
    @Autowired DaoSupport daoSupport;

    @Override
    public String save(String obj4json) throws TException {
        Category category = JSON.toJavaObject(JSON.parseObject(obj4json), Category.class);

        QueryHelper helper = daoSupport.openQueryHelper();

        helper.setNameSpace("Category.saveOne").setValue(category)
                .dao().save();

        helper.clear();
        if (category.getCtgattrs() != null && category.getCtgattrs().size() > 0){
            helper.setNameSpace("CtgAttribute.saveMany").setValue(category.getCtgattrs())
                    .dao().save();
        }
        return category.getId().toString();
    }

    @Override
    public void deleteOne(long id) throws TException {
        daoSupport.openQueryHelper()
                .setNameSpace("Category.deleteOne").setValue(id)
                .dao().deleteOne();
    }

    @Override
    public String findOne(long id) throws TException {
        Category category = daoSupport.openQueryHelper()
                .setNameSpace("Category.findOne").setValue(id)
                .dao().findOne();
        return JSON.toJSONString(category);
    }

    @Override
    public String findTop() throws TException {
        List<Category> categories = daoSupport.openQueryHelper()
                .setNameSpace("Category.findTop")
                .dao().find();
        for (Category ctg : categories){
            setChildren(ctg);
        }
        return JSON.toJSONString(categories);
    }

    @Override
    public String findChildrenByParent(long pid) throws TException {
//        logger.info(String.format(">>>findChildrenByParent>>>pid>>>%d", pid));
        List<Category> categories = daoSupport.openQueryHelper()
                .setNameSpace("Category.findByParent")
                .setValue(pid)
                .dao().find();
        for (Category ctg : categories){
            setChildren(ctg);
        }
        return JSON.toJSONString(categories);
    }

    public void setChildren(Category category){
        List<Category> children = daoSupport.getQueryHelper()
                .setNameSpace("Category.findChildren")
                .setValue(category.getId())
                .dao().find();
        if (children == null || children.size()==0)
            return;
        for (Category child : children){
            setChildren(child);
        }
        category.setChildren(children);
    }
}
